﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     qtcpsocketclient.h
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-09 00:47
///////////////////////////////////////////////////////////////////////////
#ifndef QTCPSOCKETCLIENT_H
#define QTCPSOCKETCLIENT_H

#include <QTcpSocket>
#include <QThread>
#include <QTimer>
class QTcpSocketConnectionC;

class QTcpSocketClient : public QThread
{
    Q_OBJECT
public:
    explicit QTcpSocketClient(QObject *parent = nullptr);
    ~QTcpSocketClient();
//    uint64_t addConnection(QString serverAddress,uint16_t port);//添加连接
//    void upConnection(uint64_t connectionId,QString serverAddress,uint16_t port);
//    void delConnection(uint64_t connectionId);
//    void sendData(uint64_t connectionId,QByteArray data);
signals:
//    void signal_addConnection(uint64_t connectionId,QString serverAddress,uint16_t port);
//    void signal_upConnection(uint64_t connectionId,QString serverAddress,uint16_t port);
//    void signal_delConnection(uint64_t connectionId);

    void signal_connectionState(uint64_t connectionId,bool connected);
//    void signal_sendData(uint64_t connectionId,QByteArray data);//收到数据
    void signal_recvData(uint64_t connectionId,QByteArray data);//收到数据
public slots:
    void slot_addConnection(uint64_t connectionId,QString serverAddress,uint16_t port);
    void slot_upConnection(uint64_t connectionId,QString serverAddress,uint16_t port);
    void slot_delConnection(uint64_t connectionId);
public slots:
    void slot_sendData(uint64_t connectionId,QByteArray data);
private:
    QMap<uint64_t,QTcpSocketConnectionC*> connectionMap;
protected:
    void run() override;
};


/**
 * @brief The QTcpSocketConnectionC class TCP连接（客户端）
 */
class QTcpSocketConnectionC : public QThread
{
    Q_OBJECT
public:
    explicit QTcpSocketConnectionC(uint64_t connectionId,QString address,uint16_t port,QObject *parent = nullptr);
    ~QTcpSocketConnectionC();
    void open();
    void close();
    void sendData(QByteArray data);
    void upConnection(QString serverAddress, uint16_t port);
signals:
    void signal_connectionState(uint64_t connectionId,bool connected);
    void signal_recvData(uint64_t connectionId,QByteArray byteArray);
    void signal_sendData(QByteArray byteArray);

    void signal_reStartConnectTimer();
    void signal_openSocket();
    void signal_closeSocket();
private slots:
    void slot_tcpSocketReadyRead();
    void slot_tcpSocketConnected();
    void slot_tcpSocketDisconnected();

    void slot_reStartConnectTimer();
    void slot_openSocket();
    void slot_closeSocket();

    void slot_reConnectSocket();
public slots:
    void slot_sendData(QByteArray byteArray);
private:
    quint16 getSendPackId();
    void parsingFrameData(const QByteArray &byteArray); //解析数据帧
private:
    uint64_t connectionId;//连接ID
    QString address; //地址
    uint16_t port;//端口

    bool isOpen;    //socket打开
    bool isConnected;
    QTcpSocket *tcpSocket;
    QTimer *connectTimer;
private:
    quint16 sendPackId;
    quint16 recvPackId;
    int nextFrameSerial; //下一帧序号
    QByteArray recvFrameTempData;
    QByteArray recvPackData; //临时接收数据
protected:
    void run() override;
};

#endif // QTCPSOCKETCLIENT_H
